package com.genius.adminmanager.user.service.impl;

import java.sql.SQLException;
import java.util.List;

import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.other.entity.RequestJson;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.user.entity.UserJson;
import com.genius.adminmanager.user.service.UserServiceAdapter;
import com.genius.workflow.util.ConnectionException;
import com.genius.workflow.util.DBClosedException;
import com.genius.workflow.util.DBTools;

@Service("userDeleteService")
public class UserDeleteServiceImpl extends UserServiceAdapter{

	@Override
	public JSONObject batchdel(JSONObject json, JSONObject response) {
		
		String whereField = UserJson.userId;
    	try {
			delete(whereField, json);
			response.put(ResponseJson.status, ResponseJson.status_Success);
			response.put(ResponseJson.msg, ResponseJson.msg_Del_Data_Success);
		}  catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_No_Porwer_Query_Field);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_Del_Data_Error);
		}
    	return response;
	}
	private void delete(String whereField,JSONObject json) throws ConnectionException, DBClosedException, SQLException{
		List<String[]>parms = DBTools.getDeleteListParms(whereField, json.getJSONArray(RequestJson.delData));
		String table = "t_user";
		String delSql = DBTools.getDelSqlAndWhere(table, whereField);
		DBTools.deleteBach(delSql,parms);
	}
}
